<?php

/**
 * Flickr Photosets - Tagged Page
 * Displays photos tagged with a particular user.
 * 
 * @author Brad Dougherty <bdougherty@gmail.com>
 * @version 8.5.22
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 3.0
 */

/*
Require the function library
This will include all other necessary files
*/
require_once 'lib.php';

/*
Initialize Flickr Photosets and Flickr.
This will create the Flickr object without using the cache.
	(There is still a delay when you tag sometimes, the tag
	is not always immediately available in a search.)
*/
$fp = new FlickrPhotosetsCanvas(FlickrPhotosetsCanvas::uid_from_url, false, false, false);
$fp->get_info();
$fp->init_flickr(false, false, false, true);
$page = (empty($_GET['p'])) ? 1 : $_GET['p'];
$fp->page = ($page < 1) ? 1 : $page;

/*
Photos of a user with another user
?uid=1234&m=1235
*/
if (isset($_GET['w'])) {
	
	$uid2 = $_GET['w'];
	
	// Configure strings
	$subject = '<fb:name uid="'.$uid2.'" useyou="true" capitalize="true" linked="false" /> and <fb:name uid="'.$fp->uid.'" useyou="true" capitalize="true" linked="false" />';
	
	// Photos of specified user and another person
	$photos = $fp->get_tagged_photos_of($fp->uid, $uid2);
	
	// Get some counts
	$photo_count = $photos['total'];
	
	$photos_of_prefix = 'You and ';
	
}
/*
Photos of a single user
?uid=1234
*/
else {
	
	// Configure strings
	$subject = '<fb:name uid="'.$fp->uid.'" useyou="true" capitalize="true" linked="false" />';
	
	// Get only the tagged photos of the specified user
	if (isset($_GET['view'])) {
		$photos = $fp->get_tagged_photos_of($fp->uid);
	}
	else {
		$photos = $fp->get_tagged_photos_of($fp->uid, null, true);
	}
	
	// Split the array into those added by user and by others
	$photos_by_self = array();
	$photos_by_others = array();
	foreach ($photos['photo'] as $photo) {
		if ($photo['owner'] == $fp->nsid) {
			array_push($photos_by_self, $photo);
		}
		else {
			array_push($photos_by_others, $photo);
		}
	}
	
	// Get some counts
	$photo_count = $photos['total'];
	$self_photos_count = count($photos_by_self);
	$others_photos_count = count($photos_by_others);
	
	if ($_GET['view'] == 'user') {
		
		// Get page information
		$page_count = ceil($photo_count / $fp->photos_per_page);
		$first_photo = 1 + ($fp->photos_per_page * ($fp->page - 1));
		$last_photo = $first_photo + ($fp->photos_per_page - 1);
		$last_photo = ($last_photo > $photo_count) ? $photo_count : $last_photo;
		
		$title_extra = ' Added by '.$subject;
		
	}
	else if ($_GET['view'] == 'others') {
		
		// Get page information
		$page_count = ceil($photo_count / $fp->photos_per_page);
		$first_photo = 1 + ($fp->photos_per_page * ($fp->page - 1));
		$last_photo = $first_photo + ($fp->photos_per_page - 1);
		$last_photo = ($last_photo > $photo_count) ? $photo_count : $last_photo;
		
		$title_extra = ' Added by Others';
		
	}
	else {
		
		/*
		Slice the arrays
		3 rows for self, 2 rows for other
		*/
		$photos_by_self = array_slice($photos_by_self, 0, 12);
		$photos_by_others = array_slice($photos_by_others, 0, 8);
		
	}
	
}

$photo_count = ($photo_count == 0) ? 'No' : $photo_count;

?>

<fb:title>Flickr photos of <?=$fp->get_name_fbml(false, false)?></fb:title>
<?=$fp->get_stylesheet('photos_of')?>

<fb:if-can-see uid="<?=$fp->uid?>">
	
	<fb:mediaheader uid="<?=$fp->uid?>">
		<fb:header-title><span style="color:#3e79c0;">Flick<span style="color:#d760a9;">r</span></span> photos of <?=$subject?><?=$title_extra?></fb:header-title>
	</fb:mediaheader>
	
	<?php
	// Show all photos
	if (false) {
	?>
		
		
		
	<?php }
	// Photos of two users
	else if (isset($uid2)) {
	?>
	
		<div id="photo_header" class="clearfix">
			<div class="summary">
				<?=$photo_count?> <?=($photo_count > 1) ? 'photos' : 'photo'?>
				<span class="pipe">|</span>
				<a href="<?=$fp->canvas_url?>/photos/of/<?=$fp->uid?>/">Back to Photos of <fb:name uid="<?=$fp->uid?>" firstnameonly="true" linked="false" capitalize="true" /></a>
			</div>	
			<?=Pager::getPager($fp->page, $page_count, '')?>
		</div>
		
		<div id="photo_album">
			
			<?php if ($photo_count < 1) {
				$fp->error(FlickrPhotosetsErrors::$no_tagged_photos2);
			}?>

			<table border="0" cellspacing="0">
				<tr>
					<?php
					$i = 0;
					foreach ($photos['photo'] as $photo) { ?>
						<td>
							<a href="<?=$fp->canvas_url?>/photos/<?=$fp->get_uid_from_nsid($photo['owner'])?>/<?=$photo['id']?>/of/<?=$fp->uid?>/">
								<?php if ($photo['media'] == 'video') { ?>
									<img src="http://l.yimg.com/www.flickr.com/images/video_play_icon_small.png.v1" class="video_icon" />
								<?php } ?>
								<img class="thumb" src="<?=$fp->flickr->buildPhotoUrl($photo, "thumbnail")?>" alt="<?=$photo['title']?>" title="<?=$photo['title']?>" />
							</a>
						</td>
					<?php
						$i++;
						if ($i > 3) {
							echo '</tr><tr>';
							$i = 0;
						}
					} ?>
				</tr>
			</table>
				
		</div>
		
	<?php }
	// Photos of a single user
	else { ?>
		
		<div id="photo_header" class="clearfix">
			<div class="summary">
				<?=$photo_count?> <?=($photo_count > 1) ? 'photos' : 'photo'?>
				<?php if (isset($_GET['view'])) { ?>
					<span class="pipe">|</span> <a href="<?=$fp->canvas_url?>/photos/of/<?=$fp->uid?>/">Back to Photos of <fb:name uid="<?=$fp->uid?>" firstnameonly="true" linked="false" capitalize="true" /></a>
				<?php } ?>
				<?php if (!isset($_GET['view'])) { ?>
					<span class="pipe">|</span> <a href="http://flickr.com/photos/tags/fb:user=<?=$fp->uid?>/">View these Photos on Flickr</a>
				<?php } ?>
				<?php if ($fp->uid != $fp->viewer_uid) { ?>
					<span class="pipe">|</span> <a href="<?=$fp->canvas_url?>/photos/of/<?=$fp->uid?>/and/<?=$fp->viewer_uid?>/">View Photos of You and <fb:name uid="<?=$fp->uid?>" firstnameonly="true" linked="false" capitalize="true" /></a>
				<?php } ?>
			</div>	
			<?=Pager::getPager($fp->page, $page_count, '')?>
		</div>
		
		<?php if ($photo_count < 1) {
			$fp->error(FlickrPhotosetsErrors::$no_tagged_photos);
			echo '<div class="section"><h2>But I know there are!</h2>Unfortunately, Flickr\'s search results don\'t update immediately, so you may need to wait a couple minutes for the photos to appear in the search results. Sorry!</div>';
		}?>
		
		<?php if ($self_photos_count > 0) { ?>
			<div id="photo_album">
				<?php if (!isset($_GET['view'])) { ?>
					<p class="info_message">
						Showing <?=count($photos_by_self)?> of <?=$self_photos_count?> <?=(count($photos_by_self) > 1)? 'photos' : 'photo' ?> added by <fb:name uid="<?=$fp->uid?>" firstnameonly="true" linked="false" capitalize="true" />.
						<?php if ($self_photos_count > count($photos_by_self)) {
							echo ' <a href="'.$fp->canvas_url.'/photos/of/'.$fp->uid.'/by/user/">See All</a>';
						} ?>
					</p>
				<?php } ?>

				<table border="0" cellspacing="0">
					<tr>
						<?php
						$i = 0;
						foreach ($photos_by_self as $photo) { ?>
							<td>
								<a href="<?=$fp->canvas_url?>/photos/<?=$fp->get_uid_from_nsid($photo['owner'])?>/<?=$photo['id']?>/of/<?=$fp->uid?>/">
									<?php if ($photo['media'] == 'video') { ?>
										<img src="http://l.yimg.com/www.flickr.com/images/video_play_icon_small.png.v1" class="video_icon" />
									<?php } ?>
									<img class="thumb" src="<?=$fp->flickr->buildPhotoUrl($photo, "thumbnail")?>" alt="<?=$photo['title']?>" title="<?=$photo['title']?>" />
								</a>
							</td>
						<?php
							$i++;
							if ($i > 3) {
								echo '</tr><tr>';
								$i = 0;
							}
						} ?>
					</tr>
				</table>	
			</div>
		<?php } ?>

		<?php if ($others_photos_count > 0) { ?>
			<div id="photo_album">
				<?php if (!isset($_GET['view'])) { ?>
					<p class="info_message">
						Showing <?=count($photos_by_others)?> of <?=$others_photos_count?> <?=(count($photos_by_others) > 1)? 'photos' : 'photo' ?> added by Others.
						<?php if ($others_photos_count > count($photos_by_others)) {
							echo ' <a href="'.$fp->canvas_url.'/photos/of/'.$fp->uid.'/by/user/">See All</a>';
						} ?>
					</p>
				<?php } ?>

				<table border="0" cellspacing="0">
					<tr>
						<?php
						$i = 0;
						foreach ($photos_by_others as $photo) { ?>
							<td>
								<a href="<?=$fp->canvas_url?>/photos/<?=$fp->get_uid_from_nsid($photo['owner'])?>/<?=$photo['id']?>/of/<?=$fp->uid?>/">
									<?php if ($photo['media'] == 'video') { ?>
										<img src="http://l.yimg.com/www.flickr.com/images/video_play_icon_small.png.v1" class="video_icon" />
									<?php } ?>
									<img class="thumb" src="<?=$fp->flickr->buildPhotoUrl($photo, "thumbnail")?>" alt="<?=$photo['title']?>" title="<?=$photo['title']?>" />
								</a>
							</td>
						<?php
							$i++;
							if ($i > 3) {
								echo '</tr><tr>';
								$i = 0;
							}
						} ?>
					</tr>
				</table>	
			</div>
		<?php } ?>
		
	<?php } ?>
	
	<div id="photo_footer_bar" class="bar clearfix footer_bar">
		<?=Pager::getPager($fp->page, $page_count, '')?>
	</div>
	
<fb:else>
	
	<?=$fp->error(FlickrPhotosetsErrors::$not_friends_tagged)?>
	
</fb:else></fb:if-can-see>

<?=$fp->echo_footer(true)?>